package com.tjp.eight.ui.fragment.sub;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tjp.eight.R;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 * Created by Administrator on 2017/2/17.
 */
public class SubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<SubItemModel.ItemModel> models;
    private SubThisSub subThisSub;
    private int type = 1;


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SubViewHolder(LayoutInflater.from(context).inflate(R.layout.item_sub_me, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final int pos = position;
        final SubItemModel.ItemModel caid = models.get(position);

        ((SubViewHolder) holder).tv_views.setText("" + StringUtils.toWan(models.get(position).subscibe));
        ((SubViewHolder) holder).tv_sub_name.setText("" + models.get(position).name);
        Glide.with(context).load(models.get(position).cover_path).placeholder(R.drawable.loading).error(R.drawable.loading).into(((SubViewHolder) holder).iv_sub_pic);
        if (type == 1) {
            ((SubViewHolder) holder).iv_is_up.setVisibility(View.VISIBLE);
        } else {
            ((SubViewHolder) holder).iv_is_up.setVisibility(View.INVISIBLE);
        }
        if (models.get(position).if_new == 1) {
            ((SubViewHolder) holder).iv_is_new.setVisibility(View.VISIBLE);
        } else {
            ((SubViewHolder) holder).iv_is_new.setVisibility(View.INVISIBLE);
        }
        ((SubViewHolder) holder).iv_is_sub.setImageBitmap(type == 1 ?
                BitmapUtils.getBitmap(context, R.drawable.subscribe) : BitmapUtils.getBitmap(context, R.drawable.un_subscribe));
        ((SubViewHolder) holder).iv_is_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pos == 0) return;
                SubItemModel.ItemModel model = models.get(pos);
                models.remove(pos);
                notifyItemRemoved(pos);
                models.add(0, model);
                notifyItemInserted(0);
                notifyItemRangeChanged(0, pos + 1);
            }
        });

        ((SubViewHolder) holder).iv_is_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (subThisSub != null) {
                    subThisSub.sub(caid);
                }
            }
        });
        ((SubViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((SubViewHolder) holder).iv_is_new.setVisibility(View.INVISIBLE);
                ActivityUtils.startSubDetails(context, models.get(pos).name, models.get(pos).caid);
            }
        });

    }


    class SubViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_sub_pic;
        ImageView iv_is_sub;
        ImageView iv_is_up;
        ImageView iv_is_new;
        TextView tv_views;
        TextView tv_sub_name;
        View itemView;

        public SubViewHolder(View view) {
            super(view);
            itemView = view;
            iv_sub_pic = (ImageView) view.findViewById(R.id.iv_sub_pic);
            iv_is_sub = (ImageView) view.findViewById(R.id.iv_is_sub);
            iv_is_up = (ImageView) view.findViewById(R.id.iv_is_up);
            iv_is_new = (ImageView) view.findViewById(R.id.iv_is_new);
            tv_views = (TextView) view.findViewById(R.id.tv_views);
            tv_sub_name = (TextView) view.findViewById(R.id.tv_sub_name);
        }
    }

    @Override
    public int getItemCount() {
        return models.size();
    }

    public SubAdapter(Context context) {
        this.context = context;
        models = new ArrayList<>();
    }

    public void addData(ArrayList<SubItemModel.ItemModel> itemModels) {
        if (itemModels == null || itemModels.isEmpty()) return;

        for (SubItemModel.ItemModel itemModel : itemModels) {
            models.add(itemModel);
        }
        notifyDataSetChanged();
    }

    public void addData(SubItemModel.ItemModel itemModels) {
        if (itemModels == null) return;

        boolean canAdd = true;
        for (SubItemModel.ItemModel model : models) {
            if (itemModels.caid.equals(model.caid)) {
                canAdd = false;
                break;
            }
        }
        if (models.isEmpty()) {
            canAdd = true;
        }
        if (canAdd) {
            models.add(itemModels);
            notifyItemInserted(getItemCount());
        }
    }

    public void removeData(SubItemModel.ItemModel currentSub2) {
        if (currentSub2 == null) return;

        for (SubItemModel.ItemModel m : models) {
            if (m.caid.equals(currentSub2.caid)) {
                int index = models.indexOf(m);
                models.remove(m);
                notifyItemRemoved(index);
                notifyItemRangeChanged(index, models.size());
                break;
            }
        }
    }

    public void cData(ArrayList<SubItemModel.ItemModel> itemModels) {
        if (itemModels == null || itemModels.isEmpty()) return;

        models.clear();
        for (SubItemModel.ItemModel itemModel : itemModels) {
            models.add(itemModel);
        }
        notifyDataSetChanged();
    }


    public void setType(int type) {
        this.type = type;
    }

    public void setSubThisSub(SubThisSub subThisSub) {
        this.subThisSub = subThisSub;
    }


    public interface SubThisSub {
        void sub(SubItemModel.ItemModel model);
    }
}
